{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 1\n",
    "def trace(func):\n",
    "    def wrapper(*args, **kwargs):\n",
    "        result = func(*args, **kwargs)\n",
    "        print('%s(%r, %r) -> %r' %\n",
    "              (func.__name__, args, kwargs, result))\n",
    "        return result\n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 2\n",
    "@trace\n",
    "def fibonacci(n):\n",
    "    \"\"\"Return the n-th Fibonacci number\"\"\"\n",
    "    if n in (0, 1):\n",
    "        return n\n",
    "    return (fibonacci(n - 2) + fibonacci(n - 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 3\n",
    "def fibonacci(n):\n",
    "    \"\"\"Return the n-th Fibonacci number\"\"\"\n",
    "    if n in (0, 1):\n",
    "        return n\n",
    "    return (fibonacci(n - 2) + fibonacci(n - 1))\n",
    "\n",
    "fibonacci = trace(fibonacci)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fibonacci((1,), {}) -> 1\n",
      "fibonacci((0,), {}) -> 0\n",
      "fibonacci((1,), {}) -> 1\n",
      "fibonacci((2,), {}) -> 1\n",
      "fibonacci((3,), {}) -> 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Example 4\n",
    "fibonacci(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<function trace.<locals>.wrapper at 0x103e63d90>\n"
     ]
    }
   ],
   "source": [
    "# Example 5\n",
    "print(fibonacci)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\x80\\x03c__main__\\nmy_func\\nq\\x00.'\n"
     ]
    }
   ],
   "source": [
    "# Example 6\n",
    "try:\n",
    "    # Example of how pickle breaks\n",
    "    import pickle\n",
    "    \n",
    "    def my_func():\n",
    "        return 1\n",
    "    \n",
    "    # This will be okay\n",
    "    print(pickle.dumps(my_func))\n",
    "    \n",
    "    @trace\n",
    "    def my_func2():\n",
    "        return 2\n",
    "    \n",
    "    # This will explode\n",
    "    print(pickle.dumps(my_func2))\n",
    "except:\n",
    "    logging.exception('Expected')\n",
    "else:\n",
    "    assert False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function wrapper in module __main__:\n",
      "\n",
      "wrapper(*args, **kwargs)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Example 7\n",
    "help(fibonacci)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 8\n",
    "from functools import wraps\n",
    "def trace(func):\n",
    "    @wraps(func)\n",
    "    def wrapper(*args, **kwargs):\n",
    "        result = func(*args, **kwargs)\n",
    "        print('%s(%r, %r) -> %r' %\n",
    "              (func.__name__, args, kwargs, result))\n",
    "        return result\n",
    "    return wrapper\n",
    "\n",
    "@trace\n",
    "def fibonacci(n):\n",
    "    \"\"\"Return the n-th Fibonacci number\"\"\"\n",
    "    if n in (0, 1):\n",
    "        return n\n",
    "    return (fibonacci(n - 2) +\n",
    "            fibonacci(n - 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function fibonacci in module __main__:\n",
      "\n",
      "fibonacci(n)\n",
      "    Return the n-th Fibonacci number\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Example 9\n",
    "help(fibonacci)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
